package cn.edu.henu.team.ui_login;

import butterknife.BindView;
import butterknife.OnClick;
import cn.edu.henu.team.R;
import cn.edu.henu.team.constant.ModelConstant;
import cn.edu.henu.team.fragments.HomeActivity;
import cn.edu.henu.team.widget.OwlView;
import cn.edu.henu.team.widget.VerifyCodeButton;
import cn.edu.henu.team.utils.RegexUtils;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.henu.team.base.BaseActivity;
import com.google.android.material.textfield.TextInputLayout;
import com.mob.MobSDK;

import org.json.JSONException;
import org.json.JSONObject;

public class OtpLoginActivity extends BaseActivity {
    EventHandler handler;
    @BindView(R.id.otplogin_owl_view)
    OwlView mOwlView;
    @BindView(R.id.otplogin_et_phone)
    TextInputLayout userphone;
    @BindView(R.id.otplogin_get_code)
    TextInputLayout usercode;
    @BindView(R.id.otplogin_btn_otplogin)
    Button userotplogin;
    @BindView(R.id.otplogin_tv_sign)
    TextView usersign;
    @BindView(R.id.otplogin_tv_phone)
    TextView userexitlogin;
    @BindView(R.id.btn_otplogin_get_code)
    VerifyCodeButton usergetcode;

    private EditText mEdtPhone;
    private EditText mEdtCode;

    @Override
    protected void initView() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_otp_login;
    }

    @Override
    protected void initData() {
        MobSDK.init(this, ModelConstant.MOB_APPKEY, ModelConstant.MOB_APPSECRET);

        mEdtPhone = userphone.getEditText();
        mEdtCode = usercode.getEditText();

        mEdtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!RegexUtils.checkPhone(phone)) {
                    mEdtPhone.setError("请输入正确的手机号");
                } else {
                    usercode.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        mEdtCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String code = charSequence.toString();
                if (!RegexUtils.checkCode(code)) {
                    usercode.setError("请输入正确的验证码");
                } else {
                    usercode.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        handler = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(OtpLoginActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(OtpLoginActivity.this, HomeActivity.class);
                                startActivity(intent);
                            }
                        });

                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(OtpLoginActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    }
                } else {
                    ((Throwable) data).printStackTrace();
                    Throwable throwable = (Throwable) data;
                    try {
                        JSONObject obj = new JSONObject(throwable.getMessage());
                        final String des = obj.optString("detail");
                        if (!TextUtils.isEmpty(des)) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(OtpLoginActivity.this, "提交错误信息", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        };
        SMSSDK.registerEventHandler(handler);
    }

    /**
     * 监听点击事件
     *
     * @param view
     */
    @OnClick({R.id.otplogin_tv_sign, R.id.otplogin_btn_otplogin, R.id.btn_otplogin_get_code, R.id.otplogin_tv_phone})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.otplogin_btn_otplogin:  //核验验证码进行登录
                otplogin();
                break;
            case R.id.btn_otplogin_get_code:  //获取验证码
                play();
                break;
            case R.id.otplogin_tv_sign:  //sign
                startActivity(new Intent(mContext, RegisterActivity.class));
                finish();
                break;
            case R.id.otplogin_tv_phone:  //返回登录界面
                startActivity(new Intent(mContext, LoginActivity.class));
                finish();
                break;
            default:
                break;
        }
    }

    //提交验证
    public void otplogin() {
        String code = mEdtCode.getText().toString();
        String phone = mEdtPhone.getText().toString();
        Log.d(TAG, "验证码" + code);
        Log.d(TAG, "手机号" + phone);
        SMSSDK.submitVerificationCode("86", phone, code);
    }

    //点击发送验证码
    public void play() {
        //获取验证码
        String phone = mEdtPhone.getText().toString();
        Log.d(TAG, "手机号" + phone);
        SMSSDK.getVerificationCode("86", phone);
    }
}